package ch.elexis.easy.ui;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.FormToolkit;

import ch.elexis.core.admin.InsufficientRightsException;
import ch.elexis.core.data.IActionDefinition;

public class ActionsPanel extends Composite {
	FormToolkit tk = new FormToolkit(Display.getCurrent());
	
	public ActionsPanel(final Composite parent, IActionDefinition... actions){
		super(parent, SWT.NONE);
		setLayout(new GridLayout());
		for (IActionDefinition action : actions) {
			Button b = tk.createButton(this, action.getText(), SWT.PUSH);
			b.setData(action);
			b.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent e){
					try {
						((IActionDefinition) e.widget.getData()).execute(this);
					} catch (InsufficientRightsException e1) {
						MessageDialog.openError(parent.getShell(), "Insufficient Rights",
							"You don't have sufficient rights to execute this method");
					}
				}
				
			});
			b.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
			// GridDataFactory.fillDefaults().applyTo(b);
		}
	}
}
